#region Copyright (c) 2003, newtelligence AG. All rights reserved.
/*
// Copyright (c) 2003, newtelligence AG. (http://www.newtelligence.com)
// Original BlogX Source Code: Copyright (c) 2003, Chris Anderson (http://simplegeek.com)
// All rights reserved.
//  
// Redistribution and use in source and binary forms, with or without modification, are permitted 
// provided that the following conditions are met: 
//  
// (1) Redistributions of source code must retain the above copyright notice, this list of 
// conditions and the following disclaimer. 
// (2) Redistributions in binary form must reproduce the above copyright notice, this list of 
// conditions and the following disclaimer in the documentation and/or other materials 
// provided with the distribution. 
// (3) Neither the name of the newtelligence AG nor the names of its contributors may be used 
// to endorse or promote products derived from this software without specific prior 
// written permission.
//      
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS 
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -------------------------------------------------------------------------
//
// Original BlogX source code (c) 2003 by Chris Anderson (http://simplegeek.com)
// 
// newtelligence is a registered trademark of newtelligence Aktiengesellschaft.
// 
// For portions of this software, the some additional copyright notices may apply 
// which can either be found in the license.txt file included in the source distribution
// or following this notice. 
//
*/
#endregion

using System;
using CookComputing.XmlRpc;
using newtelligence.DasBlog.Runtime;
using newtelligence.DasBlog.Web.Services.Blogger;
using newtelligence.DasBlog.Web.Services.MovableType;
using newtelligence.DasBlog.Web.Services.MetaWeblog;
using System.Reflection;
using System.Collections;
using System.Web;
using newtelligence.DasBlog.Web.Core;

namespace newtelligence.DasBlog.Web.Services
{
	[XmlRpcService(Name="DasBlog Blogger Access Point", Description="Implementation of Blogger XML-RPC Api")]
	public class BloggerAPI : XmlRpcService, IBlogger, IMovableType, IMetaWeblog
	{
		SiteConfig siteConfig;
        IBlogDataService dataService;
		ILoggingDataService logService;

        public BloggerAPI()
		{
            this.siteConfig = SiteConfig.GetSiteConfig();
			this.logService = LoggingDataServiceFactory.GetService(HttpContext.Current.Server.MapPath(siteConfig.LogDir));
            this.dataService = BlogDataServiceFactory.GetService(HttpContext.Current.Server.MapPath(siteConfig.ContentDir), logService );
       	}

        private string noNull( string s )
        {
            if ( s == null )
                return "";
            else
                return s;
        }

		bool IBlogger.blogger_deletePost(string appKey, string postid, string username, string password, bool publish)
		{
            if ( !siteConfig.EnableBloggerApi )
            {
                throw new ServiceDisabledException();
            }

            UserToken token = SiteSecurity.Login(username, password);
            if (token == null)
            {
                throw new System.Security.SecurityException();
            }

            //dataService.DeleteEntry( postid, siteConfig.CrosspostSites );
			EditServiceImplementation.DeleteEntry(postid, siteConfig, this.logService, this.dataService);

			return true;
		}

		bool IBlogger.blogger_editPost(string appKey, string postid, string username, string password, string content, bool publish)
		{
            if ( !siteConfig.EnableBloggerApi )
            {
                throw new ServiceDisabledException();
            }
            UserToken token = SiteSecurity.Login(username, password);
            if (token == null)
            {
                throw new System.Security.SecurityException();
            }
            Entry entry = dataService.GetEntryForEdit( postid );
            if ( entry != null )
            {
                entry.Content = content;
                //entry.Description = "";
                entry.Content = entry.Content;
				entry.Author = username;
				/*
                dataService.SaveEntry( 
                    entry, 
                    (siteConfig.NotifyWebLogsDotCom || siteConfig.NotifyBloGs) ? 
                      new WeblogUpdatePingInfo(siteConfig.Title,Utils.GetBaseUrl(siteConfig),Utils.GetBaseUrl(siteConfig),Utils.GetRssUrl(siteConfig), siteConfig.NotifyWebLogsDotCom, siteConfig.NotifyBloGs) : null,
                    new PingbackInfo(
                    Utils.GetPermaLinkUrl(entry.EntryId),
                    entry.Title,
                    entry.Content,
                    siteConfig.Title));
				*/
                
				EditServiceImplementation.SaveEntry(entry, siteConfig, this.logService, this.dataService);
            }
			return true;
		}

		newtelligence.DasBlog.Web.Services.Blogger.Category[]  IBlogger.blogger_getCategories(string blogid, string username, string password)
		{
            if ( !siteConfig.EnableBloggerApi )
            {
                throw new ServiceDisabledException();
            }
            UserToken token = SiteSecurity.Login(username, password);
            if (token == null)
            {
                throw new System.Security.SecurityException();
            }
            ArrayList arrayList = new ArrayList();
            CategoryCacheEntryCollection categories = dataService.GetCategories();
            if ( categories.Count == 0 )
            {
                newtelligence.DasBlog.Web.Services.Blogger.Category bcat = new newtelligence.DasBlog.Web.Services.Blogger.Category();
                bcat.categoryid = "Front Page";
                bcat.description = "Front Page";
                bcat.htmlUrl = Utils.GetCategoryViewUrl(bcat.categoryid);
                bcat.rssUrl = Utils.GetRssCategoryUrl(bcat.categoryid);
                bcat.title = noNull(bcat.description);
                arrayList.Add( bcat );
            }
            else foreach( CategoryCacheEntry cat in categories )
            {
                newtelligence.DasBlog.Web.Services.Blogger.Category bcat = new newtelligence.DasBlog.Web.Services.Blogger.Category();
                bcat.categoryid = noNull(cat.Name);
                bcat.description = noNull(cat.Name);
                bcat.htmlUrl = Utils.GetCategoryViewUrl(cat.Name);
                bcat.rssUrl = Utils.GetRssCategoryUrl(cat.Name);
                bcat.title = noNull(cat.Name);
                arrayList.Add( bcat );
            }
            return arrayList.ToArray(typeof(newtelligence.DasBlog.Web.Services.Blogger.Category)) as newtelligence.DasBlog.Web.Services.Blogger.Category[];
		}

		newtelligence.DasBlog.Web.Services.Blogger.Post IBlogger.blogger_getPost(string appKey, string postid, string username, string password)
		{
            if ( !siteConfig.EnableBloggerApi )
            {
                throw new ServiceDisabledException();
            }
            UserToken token = SiteSecurity.Login(username, password);
            if (token == null)
            {
                throw new System.Security.SecurityException();
            }
            
            Entry entry = dataService.GetEntry( postid );
            if ( entry != null )
            {
                newtelligence.DasBlog.Web.Services.Blogger.Post post = new newtelligence.DasBlog.Web.Services.Blogger.Post();
                post.content = noNull(entry.Content);
                post.dateCreated = entry.CreatedUtc;
                post.postid = noNull(entry.EntryId);
                post.userid= noNull(entry.Author);
                return post;                
            }
            else
            {
                return new newtelligence.DasBlog.Web.Services.Blogger.Post();
            }
		}

		newtelligence.DasBlog.Web.Services.Blogger.Post[] IBlogger.blogger_getRecentPosts(string appKey, 
														   string blogid, 
														   string username, 
														   string password, 
														   int numberOfPosts)
		{
            if ( !siteConfig.EnableBloggerApi )
            {
                throw new ServiceDisabledException();
            }
            UserToken token = SiteSecurity.Login(username, password);
            if (token == null)
            {
                throw new System.Security.SecurityException();
            }

            EntryCollection entries = dataService.GetEntriesForDay( DateTime.Now.ToUniversalTime(), new Util.UTCTimeZone(), null,  SiteConfig.GetSiteConfig().RssDayCount, numberOfPosts, null );
            ArrayList arrayList = new ArrayList();
            foreach (Entry entry in entries)
			{
				newtelligence.DasBlog.Web.Services.Blogger.Post post = new newtelligence.DasBlog.Web.Services.Blogger.Post();
				post.content=noNull(entry.Content);
				post.dateCreated=entry.CreatedUtc;
				post.postid=noNull(entry.EntryId);
                post.userid=noNull(entry.Author);
                
                arrayList.Add( post );
			}
            return arrayList.ToArray(typeof(newtelligence.DasBlog.Web.Services.Blogger.Post)) as newtelligence.DasBlog.Web.Services.Blogger.Post[];
		}

		string  IBlogger.blogger_getTemplate(string appKey, string blogid, string username, string password, string templateType)
		{
            if ( !siteConfig.EnableBloggerApi )
            {
                throw new ServiceDisabledException();
            }
            UserToken token = SiteSecurity.Login(username, password);
            if (token == null)
            {
                throw new System.Security.SecurityException();
            }
            return "";
		}

		newtelligence.DasBlog.Web.Services.Blogger.UserInfo  IBlogger.blogger_getUserInfo(string appKey, string username, string password)
		{
            if ( !siteConfig.EnableBloggerApi )
            {
                throw new ServiceDisabledException();
            }
            UserToken token = SiteSecurity.Login(username, password);
            if (token == null)
            {
                throw new System.Security.SecurityException();
            }
            
            newtelligence.DasBlog.Web.Services.Blogger.UserInfo userInfo = new newtelligence.DasBlog.Web.Services.Blogger.UserInfo();
            userInfo.email = noNull(siteConfig.Contact);
            userInfo.url = noNull(siteConfig.Root);
            userInfo.firstname="";
            userInfo.lastname="";
            userInfo.nickname="";
            return userInfo;
		}

		newtelligence.DasBlog.Web.Services.Blogger.BlogInfo[]  IBlogger.blogger_getUsersBlogs(string appKey, string username, string password)
		{
            if ( !siteConfig.EnableBloggerApi )
            {
                throw new ServiceDisabledException();
            }
            UserToken token = SiteSecurity.Login(username, password);
            if (token == null)
            {
                throw new System.Security.SecurityException();
            }
            
            BlogInfo[] blogs = new BlogInfo[1];
			BlogInfo blog = new BlogInfo();
			blog.blogid=noNull(siteConfig.Root);
			blog.blogName=noNull(siteConfig.Title);
			blog.url=noNull(siteConfig.Root);
            blogs[0]=blog;
			return blogs;
		}

		string IBlogger.blogger_newPost(
			string appKey, 
			string blogid, 
			string username, 
			string password, 
			string content, 
			bool publish)
		{
            if ( !siteConfig.EnableBloggerApi )
            {
                throw new ServiceDisabledException();
            }
            UserToken token = SiteSecurity.Login(username, password);
            if (token == null)
            {
                throw new System.Security.SecurityException();
            }
            
            Entry newPost = new Entry();
            newPost.Initialize();
			newPost.CreatedUtc = DateTime.Now.ToUniversalTime();
			newPost.Title = "";
            newPost.Description = "";
            newPost.Content = content;
			newPost.Author = username;
            
			/*
            dataService.SaveEntry( 
                newPost, 
                (siteConfig.NotifyWebLogsDotCom || siteConfig.NotifyBloGs) ? 
                new WeblogUpdatePingInfo(siteConfig.Title,Utils.GetBaseUrl(siteConfig),Utils.GetBaseUrl(siteConfig),Utils.GetRssUrl(siteConfig), siteConfig.NotifyWebLogsDotCom, siteConfig.NotifyBloGs) : null,
                new PingbackInfo(
                  Utils.GetPermaLinkUrl(newPost.EntryId),
                  newPost.Title,
                  newPost.Content,
                  siteConfig.Title));
            // give the XSS upstreamer a hint that things have changed
            XSSUpstreamer.TriggerUpstreaming();
			*/

			EditServiceImplementation.SaveEntry(newPost, siteConfig, this.logService, this.dataService);

			return newPost.EntryId;
		}

		bool IBlogger.blogger_setTemplate(string appKey, string blogid, string username, string password, string template, string templateType)
		{
            if ( !siteConfig.EnableBloggerApi )
            {
                throw new ServiceDisabledException();
            }
            UserToken token = SiteSecurity.Login(username, password);
            if (token == null)
            {
                throw new System.Security.SecurityException();
            }
            return false;
		}

        private newtelligence.DasBlog.Web.Services.MovableType.Category InternalGetFrontPageCategory()
        {
            newtelligence.DasBlog.Web.Services.MovableType.Category mcat = new newtelligence.DasBlog.Web.Services.MovableType.Category();
            mcat.categoryId = "Front Page";
            mcat.categoryName = "Front Page";
            //mcat.isPrimary = true;
            return mcat;
        }

        private newtelligence.DasBlog.Web.Services.MovableType.Category[] InternalGetCategoryList()
        {
            ArrayList arrayList = new ArrayList();
            CategoryCacheEntryCollection categories = dataService.GetCategories();
            if ( categories.Count == 0 )
            {
                arrayList.Add( InternalGetFrontPageCategory() );
            }
            else 
            {
                foreach (CategoryCacheEntry catEntry in categories )
                {
                    newtelligence.DasBlog.Web.Services.MovableType.Category category = new newtelligence.DasBlog.Web.Services.MovableType.Category();
                    category.categoryId=noNull(catEntry.Name);
                    category.categoryName=noNull(catEntry.Name);
                    //category.isPrimary=false;
                    arrayList.Add(category);
                }
            }
            return arrayList.ToArray(typeof(newtelligence.DasBlog.Web.Services.MovableType.Category)) as newtelligence.DasBlog.Web.Services.MovableType.Category[];
        }

        // MoveableType
        newtelligence.DasBlog.Web.Services.MovableType.Category[] IMovableType.mt_getCategoryList(string blogid, string username, string password)
        {
            if ( !siteConfig.EnableBloggerApi )
            {
                throw new ServiceDisabledException();
            }
            UserToken token = SiteSecurity.Login(username, password);
            if (token == null)
            {
                throw new System.Security.SecurityException();
            }
            return InternalGetCategoryList();
        }

        newtelligence.DasBlog.Web.Services.MovableType.Category[] IMovableType.mt_getPostCategories(string postid, string username, string password)
        {
            if ( !siteConfig.EnableBloggerApi )
            {
                throw new ServiceDisabledException();
            }
            UserToken token = SiteSecurity.Login(username, password);
            if (token == null)
            {
                throw new System.Security.SecurityException();
            }
            newtelligence.DasBlog.Web.Services.MovableType.Category[] mcats = InternalGetCategoryList();
            Entry entry = dataService.GetEntry( postid );
            if ( entry != null )
            {
                ArrayList acats = new ArrayList();
                string[] cats = entry.GetSplitCategories();
                if ( cats.Length > 0 )
                {
                    foreach( string cat in cats )
                    {
                        foreach( newtelligence.DasBlog.Web.Services.MovableType.Category mcat in mcats )
                        {
                            if ( cat == mcat.categoryId )
                            {
                                newtelligence.DasBlog.Web.Services.MovableType.Category cpcat = mcat;
                                cpcat.isPrimary = (acats.Count==0);
                                acats.Add( cpcat );
                                break;
                            }
                        }
                    }
                }
                if ( acats.Count == 0 )
                {
                    acats.Add( InternalGetFrontPageCategory() );
                }
                return acats.ToArray(typeof(newtelligence.DasBlog.Web.Services.MovableType.Category)) as newtelligence.DasBlog.Web.Services.MovableType.Category[]; 
            }
            else
            {
                return null;
            }
         }

        newtelligence.DasBlog.Web.Services.MovableType.PostTitle[] IMovableType.mt_getRecentPostTitles(string blogid, string username, string password, int numberOfPosts)
        {
            if ( !siteConfig.EnableBloggerApi )
            {
                throw new ServiceDisabledException();
            }
            UserToken token = SiteSecurity.Login(username, password);
            if (token == null)
            {
                throw new System.Security.SecurityException();
            }
            EntryCollection entries = dataService.GetEntriesForDay( DateTime.Now.ToUniversalTime(), new Util.UTCTimeZone(), null, SiteConfig.GetSiteConfig().RssDayCount, numberOfPosts, null );
            ArrayList arrayList = new ArrayList();
            foreach (Entry entry in entries)
            {
                newtelligence.DasBlog.Web.Services.MovableType.PostTitle post = new newtelligence.DasBlog.Web.Services.MovableType.PostTitle();
                post.title = noNull(entry.Title);
                post.created=entry.CreatedUtc;
                post.postid=noNull(entry.EntryId);
                post.userid=username;
                arrayList.Add( post );
            }
            return arrayList.ToArray(typeof(newtelligence.DasBlog.Web.Services.MovableType.PostTitle)) as newtelligence.DasBlog.Web.Services.MovableType.PostTitle[];
        }

        newtelligence.DasBlog.Web.Services.MovableType.TrackbackPing[] IMovableType.mt_getTrackbackPings(string postid)
        {
            if ( !siteConfig.EnableBloggerApi )
            {
                throw new ServiceDisabledException();
            }
            ArrayList arrayList = new ArrayList();
            foreach( Tracking trk in dataService.GetTrackingsFor(postid ) )
            {
                if ( trk.TrackingType == TrackingType.Trackback )
                {
                    newtelligence.DasBlog.Web.Services.MovableType.TrackbackPing tp = new newtelligence.DasBlog.Web.Services.MovableType.TrackbackPing();
                    tp.pingIP = "";
                    tp.pingTitle = noNull(trk.RefererTitle);
                    tp.pingURL = noNull(trk.PermaLink);
                    arrayList.Add( tp );
                }
            }
            return arrayList.ToArray(typeof(newtelligence.DasBlog.Web.Services.MovableType.TrackbackPing)) as newtelligence.DasBlog.Web.Services.MovableType.TrackbackPing[];
        }

        bool IMovableType.mt_publishPost(string postid, string username, string password)
        {
            if ( !siteConfig.EnableBloggerApi )
            {
                throw new ServiceDisabledException();
            }
            UserToken token = SiteSecurity.Login(username, password);
            if (token == null)
            {
                throw new System.Security.SecurityException();
            }
            return true;
        }

        bool IMovableType.mt_setPostCategories(string postid, string username, string password, newtelligence.DasBlog.Web.Services.MovableType.Category[] categories)
        {
            if ( !siteConfig.EnableBloggerApi )
            {
                throw new ServiceDisabledException();
            }
            UserToken token = SiteSecurity.Login(username, password);
            if (token == null)
            {
                throw new System.Security.SecurityException();
            }
            Entry entry = dataService.GetEntry(postid);
            if ( entry != null )
            {
                string cats = "";
                foreach( newtelligence.DasBlog.Web.Services.MovableType.Category mcat in categories )
                {
                    if ( cats.Length>0)
                        cats+=";";
                    cats += mcat.categoryId;
                }
                entry.Categories = cats;
                dataService.SaveEntry(entry);
                // give the XSS upstreamer a hint that things have changed
                XSSUpstreamer.TriggerUpstreaming();
                return true;
            }
            else
            {
                return false;
            }
        }

        string[] IMovableType.mt_supportedMethods()
        {
            if ( !siteConfig.EnableBloggerApi )
            {
                throw new ServiceDisabledException();
            }
            ArrayList arrayList = new ArrayList();
            foreach( MethodInfo method in GetType().GetMethods() )
            {
                if ( method.IsDefined(typeof(XmlRpcMethodAttribute),true) )
                {
                    XmlRpcMethodAttribute attr = method.GetCustomAttributes(typeof(XmlRpcMethodAttribute),true)[0] as XmlRpcMethodAttribute;
                    arrayList.Add(attr.Method);
                }
            }
            return arrayList.ToArray(typeof(string)) as string[];

        }

        newtelligence.DasBlog.Web.Services.MovableType.TextFilter[] IMovableType.mt_supportedTextFilters()
        {
            if ( !siteConfig.EnableBloggerApi )
            {
                throw new ServiceDisabledException();
            }
            TextFilter tf = new TextFilter();
            tf.key="default";
            tf.@value ="default";
            return new newtelligence.DasBlog.Web.Services.MovableType.TextFilter[]{tf};
        }

        // Metaweblog
        bool IMetaWeblog.metaweblog_editPost(string postid, string username, string password, newtelligence.DasBlog.Web.Services.MetaWeblog.Post post, bool publish)
        {
            if ( !siteConfig.EnableBloggerApi )
            {
                throw new ServiceDisabledException();
            }
            UserToken token = SiteSecurity.Login(username, password);
            if (token == null)
            {
                throw new System.Security.SecurityException();
            }
            Entry entry = dataService.GetEntryForEdit( postid );
            if ( entry != null )
            {
                entry.Content = post.description;
				//W.Bloggar doesn't pass in the DataCreated, 
				// so we have to check for that
				if (post.dateCreated != DateTime.MinValue)
				{
					entry.CreatedUtc = post.dateCreated;
				}
				entry.Title = post.title;
				entry.Author = username;
        
				if (post.categories != null)
				{
					// handle categories
					string categories = "";

					System.Text.StringBuilder sb = new System.Text.StringBuilder();
					bool needSemi = false;
					foreach(string category in post.categories)
					{
						if (needSemi) sb.Append(";");
						sb.Append(category);
						needSemi = true;
					}
					categories = sb.ToString();
					entry.Categories = categories;
				}            

                /*
				dataService.SaveEntry( 
                    entry, 
                    (siteConfig.NotifyWebLogsDotCom || siteConfig.NotifyBloGs) ? 
                    new WeblogUpdatePingInfo(siteConfig.Title,Utils.GetBaseUrl(siteConfig),Utils.GetBaseUrl(siteConfig),Utils.GetRssUrl(siteConfig), siteConfig.NotifyWebLogsDotCom, siteConfig.NotifyBloGs) : null,
                    new PingbackInfo(
                    Utils.GetPermaLinkUrl(entry.EntryId),
                    entry.Title,
                    entry.Content,
                    siteConfig.Title));
                // give the XSS upstreamer a hint that things have changed
                XSSUpstreamer.TriggerUpstreaming();
				*/

				EditServiceImplementation.SaveEntry(entry, siteConfig, this.logService, this.dataService);
            }
            return true;
        }

        newtelligence.DasBlog.Web.Services.MetaWeblog.CategoryInfo[] IMetaWeblog.metaweblog_getCategories(string blogid, string username, string password)
        {
            if ( !siteConfig.EnableBloggerApi )
            {
                throw new ServiceDisabledException();
            }
            UserToken token = SiteSecurity.Login(username, password);
            if (token == null)
            {
                throw new System.Security.SecurityException();
            }
            ArrayList arrayList = new ArrayList();
            CategoryCacheEntryCollection categories = dataService.GetCategories();
            if ( categories.Count == 0 )
            {
                newtelligence.DasBlog.Web.Services.MetaWeblog.CategoryInfo bcat = new newtelligence.DasBlog.Web.Services.MetaWeblog.CategoryInfo();
                bcat.categoryid = "Front Page";
                bcat.description = "Front Page";
                bcat.htmlUrl = Utils.GetCategoryViewUrl(bcat.categoryid);
                bcat.rssUrl = Utils.GetRssCategoryUrl(bcat.categoryid);
                bcat.title = noNull(bcat.description);
                arrayList.Add( bcat );
            }
            else 
            {
                foreach( CategoryCacheEntry cat in categories )
                {
                    newtelligence.DasBlog.Web.Services.MetaWeblog.CategoryInfo bcat = new newtelligence.DasBlog.Web.Services.MetaWeblog.CategoryInfo();
                    bcat.categoryid = noNull(cat.Name);
                    bcat.description = noNull(cat.Name);
                    bcat.htmlUrl = Utils.GetCategoryViewUrl(cat.Name);
                    bcat.rssUrl = Utils.GetRssCategoryUrl(cat.Name);
                    bcat.title = noNull(cat.Name);
                    arrayList.Add( bcat );
                }
            }
            return arrayList.ToArray(typeof(newtelligence.DasBlog.Web.Services.MetaWeblog.CategoryInfo)) as newtelligence.DasBlog.Web.Services.MetaWeblog.CategoryInfo[];
        }

        newtelligence.DasBlog.Web.Services.MetaWeblog.Post IMetaWeblog.metaweblog_getPost(string postid, string username, string password)
        {
            if ( !siteConfig.EnableBloggerApi )
            {
                throw new ServiceDisabledException();
            }
            UserToken token = SiteSecurity.Login(username, password);
            if (token == null)
            {
                throw new System.Security.SecurityException();
            }
            
            Entry entry = dataService.GetEntry( postid );
            if ( entry != null )
            {
                newtelligence.DasBlog.Web.Services.MetaWeblog.Post post = new newtelligence.DasBlog.Web.Services.MetaWeblog.Post();
                post.description = noNull(entry.Content);
                post.dateCreated = entry.CreatedUtc;
                post.title = noNull(entry.Title);
                post.permalink = Utils.GetPermaLinkUrl(entry);
                //post.postid = noNull(entry.EntryId);
                //post.categories = entry.GetSplitCategories();
                
                return post;                
            }
            else
            {
                return new newtelligence.DasBlog.Web.Services.MetaWeblog.Post();
            }
        }

        newtelligence.DasBlog.Web.Services.MetaWeblog.Post[] IMetaWeblog.metaweblog_getRecentPosts(string blogid, string username, string password, int numberOfPosts)
        {
            if ( !siteConfig.EnableBloggerApi )
            {
                throw new ServiceDisabledException();
            }
            UserToken token = SiteSecurity.Login(username, password);
            if (token == null)
            {
                throw new System.Security.SecurityException();
            }

            EntryCollection entries = dataService.GetEntriesForDay( DateTime.Now.ToUniversalTime(), new Util.UTCTimeZone(), null, SiteConfig.GetSiteConfig().RssDayCount, numberOfPosts, null );
            ArrayList arrayList = new ArrayList();
            foreach (Entry entry in entries)
            {
                newtelligence.DasBlog.Web.Services.MetaWeblog.Post post = new newtelligence.DasBlog.Web.Services.MetaWeblog.Post();
                post.description=noNull(entry.Content);
                post.dateCreated=entry.CreatedUtc;
                post.title = noNull(entry.Title);
                post.link = post.permalink = Utils.GetPermaLinkUrl(entry);
                post.postid = noNull(entry.EntryId);
                post.categories = entry.GetSplitCategories();                
                arrayList.Add( post );
            }
            return arrayList.ToArray(typeof(newtelligence.DasBlog.Web.Services.MetaWeblog.Post)) as newtelligence.DasBlog.Web.Services.MetaWeblog.Post[];
        }

        string IMetaWeblog.metaweblog_newPost(string blogid, string username, string password, newtelligence.DasBlog.Web.Services.MetaWeblog.Post post, bool publish)
        {
            if ( !siteConfig.EnableBloggerApi )
            {
                throw new ServiceDisabledException();
            }
            UserToken token = SiteSecurity.Login(username, password);
            if (token == null)
            {
                throw new System.Security.SecurityException();
            }
            
            Entry newPost = new Entry();
            newPost.Initialize();
            newPost.CreatedUtc = DateTime.Now.ToUniversalTime();
            newPost.Title = post.title;
            //newPost.Description = "";
            newPost.Content = post.description;
			newPost.Author = username;
			
			if (post.categories != null && post.categories.Length > 0)
			{
				// handle categories
				string categories = "";

				System.Text.StringBuilder sb = new System.Text.StringBuilder();
				bool needSemi = false;
				foreach(string category in post.categories)
				{
					if (needSemi) sb.Append(";");
					sb.Append(category);
					needSemi = true;
				}
				categories = sb.ToString();

				if (categories.Length > 0)
					newPost.Categories = categories;
			}
            
			/*
            dataService.SaveEntry( 
                newPost, 
                (siteConfig.NotifyWebLogsDotCom || siteConfig.NotifyBloGs) ? 
                new WeblogUpdatePingInfo(siteConfig.Title,Utils.GetBaseUrl(siteConfig),Utils.GetBaseUrl(siteConfig),Utils.GetRssUrl(siteConfig), siteConfig.NotifyWebLogsDotCom, siteConfig.NotifyBloGs) : null,
                new PingbackInfo(
                Utils.GetPermaLinkUrl(newPost.EntryId),
                newPost.Title,
                newPost.Content,
                siteConfig.Title));

            // give the XSS upstreamer a hint that things have changed
            XSSUpstreamer.TriggerUpstreaming();
			*/

			EditServiceImplementation.SaveEntry(newPost, siteConfig, this.logService, this.dataService);

            return newPost.EntryId;
        }
	}
}
